// 开发文档：https://uniapp.dcloud.io/uniCloud/clientdb?id=action
module.exports = {
	before: async (state, event) => {
		let db = uniCloud.database();
		let appletInfo = await db.collection('wmz-applet-info').where({
			_id: state.newData.app_id
		}).get({
			getOne: true
		})
		appletInfo = appletInfo.data[0]
		if (!appletInfo) {
			throw new Error("小程序信息查询失败");
		}
		if (!appletInfo.qrcode_path) {
			throw new Error("二维码路径为空,请联系管理员配置");
		}
		let qrcodeText = 'https://wmzcloud.wmeizi.com/com' + appletInfo.qrcode_path + "st" + '/quickpay' + state
			.newData.app_id + '?shopId=' + state.newData.shop_id + '&gathering=' + state.newData.code;
		let qrcodeErrorCorrectLevel = 'M';
		let qrcodeMargin = 2;
		let callFunctionResult = await uniCloud.callFunction({
			name: 'explain-qrcode',
			data: {
				service: 'qrcode',
				action: 'make',
				params: {
					text: qrcodeText,
					ec_level: qrcodeErrorCorrectLevel,
					margin: qrcodeMargin
				}
			}
		});
		if (callFunctionResult.result) {
			state.newData.qrcode = callFunctionResult.result
			state.newData.qrurl = qrcodeText
		}
	},
	after: async (state, event, error, result) => {
		if (error) {
			throw error
		}
		return result
	}
}